#ifndef GAMEPANEL_H
#define GAMEPANEL_H

#include <QMainWindow>
#include <QMap>
#include <QTimer>
#include <qlabel.h>
#include "GameControl.h"
#include "cardpanel.h"

QT_BEGIN_NAMESPACE
namespace Ui {
class GamePanel;
}
QT_END_NAMESPACE

class GamePanel : public QMainWindow
{
    Q_OBJECT

public:
    GamePanel(QWidget *parent = nullptr);
    ~GamePanel();

protected:
    void paintEvent(QPaintEvent* ev);
    //初始化控制类
    void Game_Control_Init();
    //更新玩家分数
    void Update_Player_Score();
    //初始化卡牌
    void Init_Card_Map();
    //裁剪卡牌
    void Crop_Image(const QPixmap& pix, int x, int y,const Card& card);
    //初始化按钮组
    void Init_Button_Group();
    //初始化上下文环境
    void Init_Player_Context();
    //初始化游戏场景
    void Init_Game_Scene();
    //处理游戏的状态
    void Game_Status_Precess(GameControl::GameStatus status);
    //发牌
    void Start_Dispatch_Card();
    //移动扑克牌
    void Card_Move_Step(Player* player,int curpos);
    //处理分发得到的扑克牌
    void Dispose_Card(Player* player,const Cards& cards);
    //更新扑克牌在窗口中的显示
    void Update_Player_Cards(Player* player);
    //定时器的处理动作
    void On_Dispatch_Card();


private:
    enum CardAlign
    {
        Horizontal,
        Vertical
    };

    struct PlayerContext
    {

        QRect card_rect_;       //玩家扑克牌显示区域
        QRect play_hand_rect_;  //出牌的区域
        CardAlign align_;       //扑克牌的对齐方式（水平/垂直）
        bool is_front_side_;    //扑克牌显示正面还是背面
        QLabel* info_;          //游戏过程中的提示信息
        QLabel* role_img_;      //玩家头像
        Cards last_cards_;      //玩家刚带出的牌
    };

private:
    Ui::GamePanel *ui;
    QPixmap background_image_;
    GameControl* game_ctl_;
    QVector<Player*> player_list_;
    QMap<Card,CardPanel*> card_map_;
    QSize card_size_;
    QPixmap card_background_image_;
    QMap<Player*,PlayerContext> context_map_;
    CardPanel* base_card_;
    CardPanel* move_card_;
    QVector<CardPanel*> last_three_cards_;
    QPoint base_card_pos_;
    GameControl::GameStatus game_status_;
    QTimer* timer_;
};
#endif // GAMEPANEL_H
